package main

import (
	"fmt"

	"github.com/sciter-sdk/go-sciter"
)

func GoCall(args ...*sciter.Value) *sciter.Value {
	obj_id := args[0].Int()
	method_name := args[1].String()
	args2 := make([]interface{}, len(args[2:]))

	fmt.Println("args:", args, args[0], args[1], args[0].Int())

	for k, arg := range args[2:] {
		fmt.Println("==>>> arg:", k, arg)
		if arg.IsInt() {
			args2[k] = arg.Int()
		} else if arg.IsString() {
			args2[k] = arg.String()
		}
	}

	fmt.Println("Call ...", args2)

	var res []interface{}
	var suc bool
	if len(args2) == 0 {
		res, suc = MethodCallByID(uint64(obj_id), method_name)
	} else {
		res, suc = MethodCallByID(uint64(obj_id), method_name, args2...)
	}

	if suc == false {
		rval := sciter.NullValue()
		return rval
	}

	ret := sciter.NewValue()
	for _, val := range res {
		v12, ok := val.(Object)
		if ok {
			r1 := sciter.NewValue()
			r1.Set("Object", v12.id())
			ret.Append(r1)
		} else {
			ret.Append(sciter.NewValue(val))
		}
	}
	return ret
}
